// doorlever.txt -  A lever that opens nearby gates. When it is walked onto, asks if the
// party wants to pull it. if they do, swaps up to 2 terrain spots. 
// Modified slightly for Mystery Manor.

// (When a terrain type is defined, you can set a swap terrain type for
// is, a terrain type that it changes to if it is opened/closed. For
// example, a closed gate's swap terrain is an open gate.

// Memory Cells - 
//   0,1 - The x,y coordinated of a gate this level opens. If both are left at 0, doesn't
//     do anything.
//   2,3 - The x,y coordinates of a second gate this lever opens. If both are left at 0, doesn't
//     do anything.
//   4,5 -  Coordinates for a stuff done flag. If these are 0 and 0, ignored. Otherwise,
//     when the lever is pulled, if the stuff done flag is 0, it becomes 1, and if the flag
//     is non-zero, it becomes 0.
//     If a flag is assigned here and is set to when, when the party enters this town later,
//     this script will open the gates.

beginterrainscript; 

variables;

short choice;

body;

beginstate INIT_STATE;
	// if the lever has been pulled already, toggle the gates
	if ((get_memory_cell(4) > 0) || (get_memory_cell(5) > 0)) {
		if (get_flag(get_memory_cell(4), get_memory_cell(5)) > 0) {
			if ((get_memory_cell(0) > 0) || (get_memory_cell(1) > 0)) {
				flip_terrain(get_memory_cell(0), get_memory_cell(1));
			}
			if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
				flip_terrain(get_memory_cell(2), get_memory_cell(3));
			}
		}
	}
break;

beginstate START_STATE;
break;

beginstate SEARCH_STATE;
	set_state_continue(10);
break;

beginstate STEP_INTO_SPOT_STATE;
	set_state_continue(10);
break;

beginstate 10;
	block_entry(1);
	reset_dialog_preset_options(2);
	choice = run_dialog(0);
	if (choice == 1)
		end();
	
	flip_terrain(my_loc_x(), my_loc_y());
	play_sound(106);
	play_sound(99);
	// print_str_color("You hear the sounds of chains and grinding gears.", 2);

	if ((get_memory_cell(0) > 0) || (get_memory_cell(1) > 0))
		flip_terrain(get_memory_cell(0), get_memory_cell(1));

	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
		flip_terrain(get_memory_cell(2), get_memory_cell(3));

	if ((get_memory_cell(4) > 0) || (get_memory_cell(5) > 0)) {
		if (get_flag(get_memory_cell(4), get_memory_cell(5)) == 0)
			set_flag(get_memory_cell(4), get_memory_cell(5), 1);
		else set_flag(get_memory_cell(4), get_memory_cell(5), 0);
	}
break;
